package cn.uppower.wordaliy.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.activity.VideoPlayActivity;
import cn.uppower.wordaliy.imageloader.GlideImageLoader;
import cn.uppower.wordaliy.model.Lesson;

/**
 * Created by elitetyc on 2018/11/26.
 * Describe:
 */
public class SouYeFragment extends Fragment {

    private List<Lesson> lessonList;

    private RecyclerView rv_kc;
    private Context context;
    private Banner banner;
    private List<Integer> images ;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= View.inflate(getActivity(), R.layout.fragment_souye,null);
        context=getActivity();
        rv_kc = view.findViewById(R.id.rv_kc);
        lessonList= new ArrayList<>();
        for (int i=0;i<50;i++){
            lessonList.add(new Lesson("基础单词教学"+i,"单词记牢靠只需"+i+"天",i+"课时(建议20天)"));
        }
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
//        linearLayoutManager.setSmoothScrollbarEnabled(true);
//        linearLayoutManager.setAutoMeasureEnabled(true);
        rv_kc.setLayoutManager(linearLayoutManager);
//        rv_kc.setHasFixedSize(true);
        rv_kc.setNestedScrollingEnabled(false);
        rv_kc.setAdapter(new MyKcAdapter());

//        初始化banner
        banner=view.findViewById(R.id.banner);
        images=new ArrayList<>();
        images.add(R.drawable.lesson_bg);
        images.add(R.drawable.lesson_bg);
        images.add(R.drawable.lesson_bg);
        //设置图片加载器
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        return view;
    }


    class MyKcAdapter extends RecyclerView.Adapter<MyKcAdapter.MyViewHolder>{


        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view=LayoutInflater.from(context).inflate(R.layout.item_lesson, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

            Lesson lesson=lessonList.get(position);
            holder.tv_lesson_title.setText(lesson.getTitle());
            holder.tv_lesson_start.setText(lesson.getStart());
            holder.tv_lesson_times.setText(lesson.getTimes());

        }

        @Override
        public int getItemCount() {
            return lessonList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder{
            private TextView tv_lesson_title,tv_lesson_start,tv_lesson_times;
            private RelativeLayout rl_lesson_video;

            public MyViewHolder(View itemView) {
                super(itemView);
                tv_lesson_title = itemView.findViewById(R.id.tv_lesson_title);
                tv_lesson_start = itemView.findViewById(R.id.tv_lesson_start);
                tv_lesson_times = itemView.findViewById(R.id.tv_lesson_times);
                rl_lesson_video = itemView.findViewById(R.id.rl_lesson_video);
                rl_lesson_video.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(context,VideoPlayActivity.class));
                    }
                });
            }
        }
    }
}
